<?php

    class Address {

        private $AddressId;
        private $LotNo;
        private $Street;
        private $Subdivision;
        private $City;
        private $PostalCode;
        private $Country;

        public function __construct($addressid, $lotno, $street, $subdivision, $city, $postalcode, $country) {
            $this->AddressId = $addressid;
            $this->LotNo = $lotno;
            $this->Street = $street;
            $this->Subdivision = $subdivision;
            $this->City = $city;
            $this->PostalCode = $postalcode;
            $this->Country = $country;
        }

        public function __get($key) {
            if(property_exists($this, $key)) {
                return $this->{$key};
            }
            else {
                return NULL;
            }
        }

        public function __set($key, $value) {
            throw new Exception('Class is immutable.');
        }

        public function ToString() {
            return $this->LotNo + ', ' + $this->Street + ', ' + $this->PostalCode + ', ' + $this->Country;
        }

        public static function InsertAddress($address) {
            Connection::Connect();
            if(($try = Address::ReadAddress($address->AddressId)) != NULL)
                    return $try->AddressId;

            $sql = mysql_query(
                "
                    INSERT INTO 
                    Address (
                        LotNo, 
                        Street, 
                        Subdivision, 
                        City, 
                        PostalCode, 
                        Country
                    ) 
                    VALUES (
                        '$address->LotNo', 
                        '$address->Street', 
                        '$address->Subdivision', 
                        '$address->City', 
                        '$address->PostalCode', 
                        '$address->Country'
                    )
                "
            );

            if($sql == FALSE)
                return NULL;

            $sql = mysql_query(
                "SELECT LAST_INSERT_ID() as 'LAST_INSERT_ID'"
            );

            if($sql == NULL)
                return NULL;

            $rows = mysql_num_rows($sql);
            if($rows == 1) {
                while($row = mysql_fetch_assoc($sql))
                {
                    return $row['LAST_INSERT_ID'];
                }
            }

            return NULL;
        }

        public static function ReadAddress($addressid) {
            Connection::Connect();
            $sql = mysql_query(
                "
                    SELECT 
                        *
                    FROM 
                        Address 
                    WHERE
                        AddressId = '$addressid'
                "
            );

            if($sql == NULL)
                return NULL;

            $rows = mysql_num_rows($sql);
            if($rows == 1) {
                while($row = mysql_fetch_assoc($sql))
                {
                    $addressid = $row['AddressId'];
                    $lotno = $row['LotNo'];
                    $street = $row['Street'];
                    $subdivision = $row['Subdivision'];
                    $city = $row['City'];
                    $postalcode = $row['PostalCode'];
                    $country = $row['Country'];
                    return new Address($addressid, $lotno, $street, $subdivision, $city, $postalcode, $country);
                }
            }
            return NULL;
        }

        public static function UpdateAddress($address) {
            Connection::Connect();
            $sql = mysql_query(
                "
                    UPDATE 
                        Address 
                    SET 
                        LotNo = '$address->LotNo', 
                        Street = '$address->Street', 
                        Subdivision = '$address->Subdivision', 
                        City = '$address->City', 
                        PostalCode = '$address->PostalCode', 
                        Country = '$address->Country'
                    WHERE 
                        AddressId = '$address->AddressId'
                " 
            );
            if($sql == FALSE)
                return NULL;

            return $address->AddressId;
        }

    }

?>